package com.coinland.study.window;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 类描述:  CustomView 背景动画
 * 创建人:  shenZ
 * 创建时间: 2020/5/9
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/5/9
 */
public class XshapeView extends View {
    private String TAG = XshapeView.class.getSimpleName();

    private Paint paint;      //画背景颜色的笔
    private int backgroudColor = 0xFFDBDBDB; //圆形头像背景色

    // 方向侧
    public enum SIDE{
        RIGHT_SIDE_E, LEFT_SIDE_S,
        LEFT_SIDE_E, RIGHT_SIDE_S,NONE
    }

    public XshapeView(Context context) {
        super(context);
        init();
        Log.e(TAG, "调用构造方法1");
    }

    public XshapeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        Log.e(TAG, "调用构造方法2");
    }

    public XshapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        Log.e(TAG, "调用构造方法3");
    }


    private void init() {
        //画背景颜色的笔
        paint = new Paint();
        paint.setColor(backgroudColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setDither(true); //设置防抖动
    }

    /**
     * 视图绘制工作
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        // 绘制控件背景
        int mViewSize = Math.min(width, height);
        //圆的半径短的二分之一作为半径
        float radius = mViewSize / 2;

        Path path = new Path();
        RectF rectF = new RectF(0, 0, mViewSize, mViewSize); // 圆弧位置

        if(mSide == SIDE.NONE  || mSide == SIDE.RIGHT_SIDE_E){
            // 画个左半圆 + 矩形
            path.addRoundRect(rectF,new float[]{radius, radius, 0, 0, 0, 0, radius, radius}, Path.Direction.CCW);
        }else if(mSide == SIDE.RIGHT_SIDE_S||mSide == SIDE.LEFT_SIDE_S){
            // 画个圆
            path.addCircle(radius, radius, radius, Path.Direction.CW);
        }else if(mSide == SIDE.LEFT_SIDE_E){
            // 画个 矩形 + 右半圆
            path.addRoundRect(rectF,new float[]{0, 0, radius, radius, radius, radius, 0, 0}, Path.Direction.CCW);
        }
        canvas.drawPath(path, paint);
    }



    /**
     * 获取控件的宽和高度  (在控件大小发生改变时调用)
     *
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    /**
     * 测量View宽高 （在View放置到父容器时调用）
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    /**
     * 计算当前View以及子View的位置
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    @Override
    public void layout(int left, int top, int right, int bottom) {
        super.layout(left, top, right, bottom);
    }


    /**
     * 去让控件重绘
     */
    @Override
    public void postInvalidate() {
        super.postInvalidate();
    }


    // 默认NONE
    private SIDE mSide  = SIDE.NONE;

    /**
     * 扩展View  或者  收缩View
     */
    public void setSideView(SIDE side){
        mSide = side;
        postInvalidate();
    }
}
